package com.jg.xmlSchema2JsonSchema.xsd;

import com.jg.xmlSchema2JsonSchema.xsd.util.XsdUtil;
import org.w3c.dom.Element;

import java.util.Comparator;
import java.util.List;

public class XsComplexType {

    // 节点名称
    private String name;
    // 节点描述
    private String annotation;
    private Element element;

    public XsComplexType(Element element) {
        this.element = element;
        getData();
    }

    private void getData() {
        name = element.getAttribute("name");
        XsAnnotation xsAnnotation = XsdUtil.getAnnotation(element);
        if (xsAnnotation != null) {
            annotation = xsAnnotation.getText();
        }
    }

    public XsSequence getSequence() {
        return XsdUtil.getSequence(element);
    }

    public List<XsAttribute> getAttributes() {
        List<XsAttribute> resultList = XsdUtil.getAttributes(element);
        resultList.sort(Comparator.comparing(XsAttribute::getName));

        return resultList;
    }

    public XsComplexContent getComplexContent() {
        return XsdUtil.getComplexContent(element);
    }

    public XsAll getAll() {
        return XsdUtil.getAll(element);
    }

    public List<XsChoice> getChoices() {
        return XsdUtil.getChoices(element);
    }

    public XsAttribute getAttribute(String name) {
        List<XsAttribute> attributes = XsdUtil.getAttributes(element);
        for (XsAttribute xa : attributes) {
            if (xa.getName().equals(name)) {
                return xa;
            }
        }
        return null;
    }

    public String getName() {
        return name;
    }

    public String getAnnotation() {
        return annotation;
    }

    public Element getXmlElement() {
        return element;
    }
	/*
	public XsRestriction getRestriction()
	{
		return XsdUtil.getRestriction(element);
	}
	*/
}
